package com.zrrd;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

/**
 * @author WanJl
 * @version 1.0
 * @title Solution
 * @description
 * @create 2024/8/23
 */
import java.util.*;
import java.util.*;
 class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}

public class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        // 创建一个虚拟头节点，并指向头节点
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode prev = dummy; // prev 是指向当前已确定没有重复的节点

        while (head != null) {
            // 如果当前节点与下一个节点值相同，向后移动，直到找到不同的值
            if (head.next != null && head.val == head.next.val) {
                while (head.next != null && head.val == head.next.val) {
                    head = head.next;
                }
                // 跳过所有重复的节点
                prev.next = head.next;
            } else {
                // 没有重复，直接移动 prev 指针
                prev = prev.next;
            }
            head = head.next;
        }
        return dummy.next;
    }
}